﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Data.SqlClient;

namespace DoAn
{
    public partial class frmTylelai : Form
    {
        SqlConnection conn;
        SqlCommand cmd;
        SqlDataAdapter da;
        DataSet ds;
        SqlCommandBuilder cmb;
        public frmTylelai()
        {
            String conStr = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
            conn = new SqlConnection(conStr);
            conn.ConnectionString = conStr;
            InitializeComponent();
        }

        private void frmTylelai_Load(object sender, EventArgs e)
        {
            //conn.Open();
            cmd = new SqlCommand("SELECT * FROM tylelai", conn);
            da = new SqlDataAdapter(cmd);
            ds = new DataSet();
            da.Fill(ds, "tylelai");
            cmb = new SqlCommandBuilder(da);
            dataGridView1.DataSource = ds.Tables[0];
            conn.Close();
        }

        private void btthem_Click(object sender, EventArgs e)
        {
            try
            {
                if (tbmatll.Text == "" || tbtentll.Text == "" || tbmuclai.Text == "")
                {
                    MessageBox.Show("Bạn chưa điền đầy đủ thông tin", "Có lỗi xảy ra!");
                    tbmatll.Focus();
                    return;
                }
                else
                {
                    conn.Open();
                    cmd = new SqlCommand("SELECT * FROM tylelai", conn);
                    da = new SqlDataAdapter(cmd);
                    ds = new DataSet();
                    da.Fill(ds, "tylelai");
                    cmb = new SqlCommandBuilder(da);
                    dataGridView1.DataSource = ds.Tables[0];
                    conn.Close();
                    DataTable dt = ds.Tables[0];
                    DataRow newRow = dt.NewRow();
                    newRow["matll"] = tbmatll.Text;
                    newRow["tllten"] = tbtentll.Text;
                    newRow["tllmuclai"] = tbmuclai.Text;

                    dt.Rows.Add(newRow);
                    da.Update(ds, "tylelai");
                    MessageBox.Show("Thêm thành công");
                }

            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
                tbmatll.Focus();

            }
        }

        private void btSua_Click(object sender, EventArgs e)
        {
            String matll = tbmatll.Text;
            String tllten = tbtentll.Text;
            String tllmuclai = tbmuclai.Text;

            int matl = Int32.Parse(matll);

            conn.Open();
            cmd = new SqlCommand("Update tylelai set tllten='" + tllten + "', tllmuclai='" + tllmuclai + "' where matll=" + matll + "", conn);
            da = new SqlDataAdapter(cmd);
            int i = cmd.ExecuteNonQuery();
            ds = new DataSet();
            da.Fill(ds, "tylelai");
            cmb = new SqlCommandBuilder(da);
            this.frmTylelai_Load(sender, e);
            conn.Close();
        }

        private void btXoa_Click(object sender, EventArgs e)
        {
            int i = dataGridView1.CurrentRow.Index;
            ds.Tables[0].Rows[i].Delete();
            da.Update(ds, "tylelai");
            MessageBox.Show("Xóa thành công");
        }

        private void btThoat_Click(object sender, EventArgs e)
        {
            frmMenu objfrmMenu = new frmMenu();
            this.Dispose();
            objfrmMenu.ShowDialog();
        }

        private void bttimkiemmatll_Click(object sender, EventArgs e)
        {
            try
            {
                if (tbtimkiemmtll.Text == "")
                {
                    MessageBox.Show("Bạn chưa điền Mã", "Có lỗi xảy ra!");
                    tbtimkiemmtll.Focus();
                    return;
                }
                else
                {

                    conn.Open();
                    cmd = new SqlCommand("SELECT * FROM tylelai where matll  = '" + tbtimkiemmtll.Text + "'", conn);
                    da = new SqlDataAdapter(cmd);
                    ds = new DataSet();
                    da.Fill(ds, "tylelai");
                    cmb = new SqlCommandBuilder(da);
                    dataGridView1.DataSource = ds.Tables[0];
                    conn.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void bttimkiemtentll_Click(object sender, EventArgs e)
        {
            try
            {
                if (tbtimkiemtentll.Text == "")
                {
                    MessageBox.Show("Bạn chưa điền Tên", "Có lỗi xảy ra!");
                    tbtimkiemtentll.Focus();
                    return;
                }
                else
                {

                    conn.Open();
                    cmd = new SqlCommand("SELECT * FROM tylelai where tllten  = '" + tbtimkiemtentll.Text + "'", conn);
                    da = new SqlDataAdapter(cmd);
                    ds = new DataSet();
                    da.Fill(ds, "tylelai");
                    cmb = new SqlCommandBuilder(da);
                    dataGridView1.DataSource = ds.Tables[0];
                    conn.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            tbmatll.Text = dataGridView1.CurrentRow.Cells[0].Value.ToString();
            tbtentll.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
            tbmuclai.Text = dataGridView1.CurrentRow.Cells[2].Value.ToString();
        }

        

    }
}
